package com.giscafer.user;

import com.giscafer.common.model.User;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Page;
import top.jwtTokenPlugin.Bean.IJwtAble;
import top.jwtTokenPlugin.service.IJwtUserService;

import java.util.Arrays;

public class UserService implements IJwtUserService {

    public static final UserService me = new UserService();

    public UserService() {
    }

    private static Kv store = Kv.create();

//    static {
//        store.set("admin",
//                new User().setForces(Arrays.asList("登录后台", "管理用户"))
//                        .setRoles(Arrays.asList("管理员", "普通用户")).setUsername("admin").setPassword("123456")
//        ).set("user",
//                new User().setForces(Arrays.asList("前台登录", "发布文章"))
//                        .setRoles(Arrays.asList("普通用户")).setUsername("user").setPassword("123456")
//        );
//    }

    /**
     * 登录验证
     *
     * @param userName
     * @param password
     * @return
     */
    public IJwtAble login(String userName, String password) {
        User user = dao.findFirst("select * from user where username='" + userName + "' and password='" + password + "'");
        if (user != null) {
            return user;
        } else {
            throw new RuntimeException("找不到用户");
        }

    }

    /**
     * 所有的 dao 对象也放在 Service 中
     */
    private static final User dao = new User().dao();

    public Page<User> paginate(int pageNumber, int pageSize) {
        return dao.paginate(pageNumber, pageSize, "select *", "from user order by id asc");
    }

    public User findById(int id) {
        return dao.findById(id);
    }

    public User findByUserName(String userName) {
        return dao.findFirst("select * from user where username='" + userName + "'");
    }

}
